<template>
  <div class="scene-mode">
    <div class="scene-header">
      <h2>场景模式</h2>
      <div class="current-scene">
        <span>当前模式：</span>
        <el-tag :type="getSceneTagType(currentScene) || 'info'" size="large">
          <el-icon><component :is="getSceneIcon(currentScene)" /></el-icon>
          {{ getSceneName(currentScene) }}
        </el-tag>
      </div>
    </div>
    
    <!-- 场景模式卡片 -->
    <div class="scene-cards">
      <div
        v-for="(config, mode) in sceneConfigs"
        :key="mode"
        class="scene-card"
        :class="{ 'active': currentScene === mode }"
        @click="setSceneMode(mode)"
      >
        <div class="scene-icon">
          <el-icon :size="32">
            <component :is="getSceneIcon(mode)" />
          </el-icon>
        </div>
        <div class="scene-info">
          <h3>{{ config.name }}</h3>
          <p>{{ config.description }}</p>
        </div>
        <div class="scene-status">
          <el-icon v-if="currentScene === mode" :size="20">
            <Check />
          </el-icon>
        </div>
      </div>
    </div>
    
    <!-- 快捷操作 -->
    <div class="quick-actions">
      <h3>快捷操作</h3>
      <div class="action-buttons">
        <el-button 
          type="success" 
          size="large"
          @click="turnOnAllDevices"
          :disabled="activeDevicesCount === devices.length"
        >
          <el-icon><Switch /></el-icon>
          开启所有设备
        </el-button>
        
        <el-button 
          type="danger" 
          size="large"
          @click="turnOffAllDevices"
          :disabled="activeDevicesCount === 0"
        >
          <el-icon><Close /></el-icon>
          关闭所有设备
        </el-button>
        
        <el-button 
          type="warning" 
          size="large"
          @click="setSceneMode('away')"
        >
          <el-icon><Location /></el-icon>
          一键离家
        </el-button>
      </div>
    </div>
    
    <!-- 场景模式说明 -->
    <div class="scene-description">
      <el-alert
        title="场景模式说明"
        type="info"
        :closable="false"
        show-icon
      >
        <template #default>
          <ul>
            <li><strong>在家模式：</strong>保持当前设备状态，适合日常使用</li>
            <li><strong>离家模式：</strong>自动关闭所有设备，节能环保</li>
            <li><strong>睡眠模式：</strong>关闭灯光，调节空调温度至适宜睡眠</li>
            <li><strong>聚会模式：</strong>开启所有灯光，调节空调至舒适温度</li>
          </ul>
        </template>
      </el-alert>
    </div>
  </div>
</template>

<script>
/* eslint-disable no-unused-vars */
import { 
  House, 
  Location, 
  Moon, 
  Star, 
  Check, 
  Switch, 
  Close 
} from '@element-plus/icons-vue'
/* eslint-enable no-unused-vars */
import { ElMessage } from 'element-plus'
import { useDeviceStore } from '../store/deviceStore.js'
import { SCENE_MODES, SCENE_CONFIGS } from '../types/device.js'

export default {
  name: 'SceneMode',
  setup() {
    const deviceStore = useDeviceStore()
    
    const sceneConfigs = SCENE_CONFIGS
    
    const getSceneIcon = (mode) => {
      const iconMap = {
        [SCENE_MODES.HOME]: House,
        [SCENE_MODES.AWAY]: Location,
        [SCENE_MODES.SLEEP]: Moon,
        [SCENE_MODES.PARTY]: Star
      }
      return iconMap[mode] || House
    }
    
    const getSceneName = (mode) => {
      return sceneConfigs[mode]?.name || '未知模式'
    }
    
    const getSceneTagType = (mode) => {
      const typeMap = {
        [SCENE_MODES.HOME]: 'success',
        [SCENE_MODES.AWAY]: 'danger',
        [SCENE_MODES.SLEEP]: 'info',
        [SCENE_MODES.PARTY]: 'warning'
      }
      return typeMap[mode] || 'info'
    }
    
    const setSceneMode = async (mode) => {
      await deviceStore.setSceneMode(mode)
      ElMessage.success(`已切换到${getSceneName(mode)}`)
    }
    
    const turnOnAllDevices = async () => {
      await deviceStore.turnOnAllDevices()
      ElMessage.success('已开启所有设备')
    }
    
    const turnOffAllDevices = async () => {
      await deviceStore.turnOffAllDevices()
      ElMessage.success('已关闭所有设备')
    }
    
    return {
      currentScene: deviceStore.currentScene,
      devices: deviceStore.devices,
      activeDevicesCount: deviceStore.activeDevicesCount,
      sceneConfigs,
      getSceneIcon,
      getSceneName,
      getSceneTagType,
      setSceneMode,
      turnOnAllDevices,
      turnOffAllDevices
    }
  }
}
</script>

<style scoped>
.scene-mode {
  padding: 20px;
}

.scene-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  flex-wrap: wrap;
  gap: 15px;
}

.scene-header h2 {
  margin: 0;
  color: #333;
}

.current-scene {
  display: flex;
  align-items: center;
  gap: 10px;
}

.scene-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 40px;
}

.scene-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
  border: 2px solid transparent;
  display: flex;
  align-items: center;
  gap: 15px;
}

.scene-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.scene-card.active {
  border-color: #409eff;
  background: linear-gradient(135deg, #f0f8ff 0%, #e6f3ff 100%);
}

.scene-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #409eff, #66b3ff);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
}

.scene-card.active .scene-icon {
  background: linear-gradient(135deg, #13ce66, #67c23a);
}

.scene-info {
  flex: 1;
}

.scene-info h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.scene-info p {
  margin: 0;
  color: #666;
  font-size: 14px;
  line-height: 1.4;
}

.scene-status {
  color: #13ce66;
  flex-shrink: 0;
}

.quick-actions {
  margin-bottom: 30px;
}

.quick-actions h3 {
  margin-bottom: 15px;
  color: #333;
}

.action-buttons {
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
}

.action-buttons .el-button {
  flex: 1;
  min-width: 150px;
}

.scene-description {
  margin-top: 20px;
}

.scene-description ul {
  margin: 10px 0 0 0;
  padding-left: 20px;
}

.scene-description li {
  margin-bottom: 8px;
  line-height: 1.5;
}

@media (max-width: 768px) {
  .scene-header {
    flex-direction: column;
    align-items: stretch;
  }
  
  .scene-cards {
    grid-template-columns: 1fr;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .action-buttons .el-button {
    min-width: auto;
  }
}
</style>
